//
//  UIButtonViewController.m
//  UITest
//
//  Created by wp w on 12-6-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UIButtonViewController.h"

@interface UIButtonViewController ()

@end

@implementation UIButtonViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)buttonDown:(id)sender
{
	NSLog(@"Button pushed down");
}

-(void)buttonRelease:(id)sender
{
	NSLog(@"Button released");
}

-(void)checkboxClick:(UIButton *)btn
{
	btn.selected = !btn.selected;
}

- (void)viewDidLoad {
	
    [super viewDidLoad];
	
	//rounded-rect button
    //UIButtonTypeRoundedRect
    //UIControlStateHighlighted
	UIButton *roundedRectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	
	CGRect buttonRect = CGRectMake(5,5,100,35);
	[roundedRectButton setFrame:buttonRect];
	
	[roundedRectButton setTitle:@"Normal" forState:UIControlStateNormal];
	[roundedRectButton setTitle:@"Highlighted" forState:UIControlStateHighlighted];
	roundedRectButton.showsTouchWhenHighlighted = YES;
	
    [roundedRectButton addTarget:self action:@selector(buttonDown:) forControlEvents:UIControlEventTouchDown];
    [roundedRectButton addTarget:self action:@selector(buttonRelease:) forControlEvents:UIControlEventTouchUpInside];
    
	
	//checkbox control
    //UIButtonTypeCustom
	UIButton *checkbox = [UIButton buttonWithType:UIButtonTypeCustom];
	
	CGRect checkboxRect = CGRectMake(135,5,36,36);
	[checkbox setFrame:checkboxRect];
	
	[checkbox setImage:[UIImage imageNamed:@"checkbox_off.png"] forState:UIControlStateNormal];
	[checkbox setImage:[UIImage imageNamed:@"checkbox_on.png"] forState:UIControlStateSelected];
	
    [checkbox addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //custom button
    //新建btn
    UIButton *doSomethingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置btnframe
    CGRect doSomethingRect = CGRectMake(5,45,200,40);
	[doSomethingButton setFrame:doSomethingRect];
    
    //设置title
    [doSomethingButton setTitle:@"自定义btn－图片可拉伸" forState:UIControlStateNormal];
    
    //新建一个可以拉伸的image，并设置normal背景
    UIImage *buttonImageNormal = [UIImage imageNamed:@"whiteButton.png"];
    UIImage *stretchableButtonImageNormal = [buttonImageNormal 
                                             stretchableImageWithLeftCapWidth:12 topCapHeight:0];

    [doSomethingButton setBackgroundImage:stretchableButtonImageNormal 
                                 forState:UIControlStateNormal];
    
    //新建一个可以拉伸的image，并设置Highlighted背景
    UIImage *buttonImagePressed = [UIImage imageNamed:@"blueButton.png"];
    UIImage *stretchableButtonImagePressed = [buttonImagePressed
                                              stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [doSomethingButton setBackgroundImage:stretchableButtonImagePressed 
                                 forState:UIControlStateHighlighted];
    
    //custom button
    UIButton *cusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect cusBtnRect = CGRectMake(5,90,200,40);
	[cusBtn setFrame:cusBtnRect];
	
    [cusBtn setTitle:@"img and title" forState:UIControlStateNormal];
	[cusBtn setImage:[UIImage imageNamed:@"checkbox_off.png"] forState:UIControlStateNormal];
    
    // Set up the button aligment properties
    [cusBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [cusBtn setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
//	cusBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//	cusBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
	
	// Set the font and color
	[cusBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[cusBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    
	
	//custom circular button
    //UIButtonTypeCustom
	UIButton *circularButton = [UIButton buttonWithType:UIButtonTypeCustom];
	
	CGRect circularRect = CGRectMake(80,220,165,164);
	[circularButton setFrame:circularRect];
	
	UIImage *buttonImage = [UIImage imageNamed:@"circular_button.png"];
	[circularButton setImage:buttonImage forState:UIControlStateNormal];
	
    [circularButton addTarget:self action:@selector(buttonDown:) forControlEvents:UIControlEventTouchUpInside];
        
	//add all the buttons to the main view
	[self.view addSubview:roundedRectButton];	
	[self.view addSubview:checkbox];	
    [self.view addSubview:doSomethingButton];
    [self.view addSubview:cusBtn];
	[self.view addSubview:circularButton];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
